package com.yt.common.utils;

import com.alibaba.simpleimage.ImageWrapper;
import com.alibaba.simpleimage.SimpleImageException;
import com.alibaba.simpleimage.render.WatermarkParameter;
import com.alibaba.simpleimage.util.ImageDrawHelper;
import com.alibaba.simpleimage.util.ImageReadHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;

public class WaterMark {
    // 添加水印 第二个参数为水印的内容，为字符串
    public static BufferedImage addWaterMark(InputStream in, String note) throws SimpleImageException {
        // 获取照片类
        ImageWrapper imageWrapper = ImageReadHelper.read(in);
        // 获取水印照片包装类
        ImageWrapper waterWrapper = new ImageWrapper(getWaterMask(note));
        // 计算水印放在哪个位置
        Point p = calculate(imageWrapper.getWidth(), imageWrapper.getHeight(), waterWrapper.getWidth(), waterWrapper.getHeight());
        // 水印属性类
        WatermarkParameter param = new WatermarkParameter(waterWrapper, 1f, (int) p.getX(), (int) p.getY());
        // // 将水印写到图片上并返回图片Buff
        return ImageDrawHelper.drawWatermark(imageWrapper.getAsBufferedImage(), param);
    }

    // 将文字生成为图片
    private static BufferedImage getWaterMask(String text) {
        Color fontColor = new Color(203, 203, 203);
        Color bgColor = new Color(0, 0, 0, 178);
        Font font = new Font("楷体", Font.PLAIN, 20);
        int[] num = getStringWidthAndHeight(text, font);
        int width = num[1]; //字符串宽度
        int height = num[0]; //字符串高度
        float alpha = 0.2f;

        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        //得到画笔对象
        Graphics2D g2d = buffImg.createGraphics();
        // 添加背景色
//        g2d.setColor(bgColor);
        g2d.setColor(bgColor);
        g2d.fillRoundRect(0, 0, width, height, 7,7);
        // ----------  增加下面的代码使得背景透明  -----------------
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2d.dispose();
        // ----------  背景透明代码结束  -----------------
        // 添加文字
        g2d = buffImg.createGraphics();
        // 设置对线段的锯齿状边缘处理
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        // 设置颜色
        g2d.setColor(fontColor);
        // 设置 Font
        g2d.setFont(font);
        //设置透明度:1.0f为透明度 ，值从0-1.0，依次变得不透明
//        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2d.drawString(text, 0, height - 5);//居中显示
        //释放资源
        g2d.dispose();
//        System.out.println("添加水印文字完成!");
        return buffImg;
    }

    // 获取文字的高和宽
    private static int[] getStringWidthAndHeight(String str, Font font) {
        JLabel label = new JLabel();
        label.setFont(font);
        FontMetrics metrics;
        int textH;
        int textW;
        label.setText(str);
        metrics = label.getFontMetrics(font);
        textH = metrics.getHeight();//字符串的高,   只和字体有关
        textW = metrics.stringWidth(label.getText());//字符串的宽
        int[] num = new int[2];
        num[0] = textH;
        num[1] = textW;
        return num;
    }

    // 计算水印放的位置
    private static Point calculate(int parentWidth, int parentHeight, int width, int height) {
        int x = (parentWidth - (int)(parentWidth * 0.05)) - width ;
        int y = (parentHeight - (int)(parentHeight * 0.1));
        return new Point(x, y);
    }
}
